package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.mscyb.bean.*;
import com.jy.adscm.operation.mscyb.bean.JqdbMaterial;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqdbMaterialMapper;
import com.jy.adscm.operation.mscyb.service.JqdbMaterialListService;
import com.jy.adscm.operation.mscyb.service.JqdbMaterialService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.purchase.bean.Purchasebom;
import com.jy.adscm.operation.purchase.mapper.mysql.PurchasebomMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-06-18
 */
@Service
public class JqdbMaterialServiceImpl extends ServiceImpl<JqdbMaterialMapper, JqdbMaterial> implements JqdbMaterialService {
   @Autowired
   CgMaterialMapper cgMaterialMapper;
   @Autowired
    JqdbMaterialListService jqdbMaterialListService;
    @Override
    public List<JqdbMaterial> selectByTid(String tid) {
        Wrapper<JqdbMaterial> wrapper =new EntityWrapper<>();
        wrapper.eq("tid",tid);

        List<JqdbMaterial> jqdbMaterials = super.selectList(wrapper);
        for (JqdbMaterial jqdbMaterial : jqdbMaterials) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqdbMaterial.getName(),null)){
                CgMaterial cgMaterial = cgMaterialMapper.selectById(jqdbMaterial.getName());
                if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial,null)){
                    jqdbMaterial.setNameName(cgMaterial.getName());
                }
            }
        }
        return jqdbMaterials;
    }

    @Override
    public Boolean approve(String tid, Token token) throws AdscmSystemException {
        //根据这个tid 查询到这个下面的所有的装配清单
        Wrapper<JqdbMaterial> jqbzMaterialWrapper=new EntityWrapper<>() ;
        jqbzMaterialWrapper.eq("tid",tid);
        List<JqdbMaterial> jqbzMaterials = super.selectList(jqbzMaterialWrapper);
        for (JqdbMaterial jqbzMaterial : jqbzMaterials) {
            if (jqbzMaterial.getIsAssembly()==1){
                continue;
            }
            //到原料库查询对应的库存
            List<JqylMaterial> jqylMaterials = jqdbMaterialListService.queryMaterial(jqbzMaterial.getName());
            double temp=0f;

            for (JqylMaterial jqylMaterial : jqylMaterials) {
                temp=temp+jqylMaterial.getRemainingnums();
            }
            if (jqbzMaterial.getNum()>temp){
                continue;
            }
            Float num = jqbzMaterial.getNum();

            for (int i = 0; i < jqylMaterials.size(); i++) {
                JqylMaterial jqylMaterial = jqylMaterials.get(0);
                Float remainingnums = jqylMaterial.getRemainingnums();
                if (remainingnums-num>0){
                    JqdbMaterialList jqdbMaterialList =new JqdbMaterialList();
                    jqdbMaterialList.setPid(jqbzMaterial.getId());
                    jqdbMaterialList.setName(jqylMaterial.getTid().toString());
                    jqdbMaterialList.setNum(num);
                    jqdbMaterialListService.insertJqdbMaterialList(jqdbMaterialList,token);
                    break;
                }else {
                    num =num-remainingnums.floatValue();
                    JqdbMaterialList jqdbMaterialList =new JqdbMaterialList();
                    jqdbMaterialList.setPid(jqbzMaterial.getId());
                    jqdbMaterialList.setName(jqylMaterial.getTid().toString());
                    jqdbMaterialList.setNum(remainingnums.floatValue());
                    jqdbMaterialListService.insertJqdbMaterialList(jqdbMaterialList,token);
                }
            }
        }
        return true;
    }
}
